<?php
/**
 * UserController.class.php
 * 用户控制器
 *
 * @author  王中艺
 * @date    2016-11-10
 */

namespace Wechat\Controller;
use Common\Util\SmsCode;

class UserController extends BaseController {

    /**
     * 用户信息获取
     * @return json 用户信息
     */
    function index(){
        $this->outOInfo(C('user'));
    }

    /**
     * 用户课程列表
     * @param   status  课程状态
     * @return json 课程列表
     */
    function courses(){
        $status     = $this->iInfo['status'] ? $this->iInfo['status'] : false;
        $page   = $this->iInfo['page'] ? $this->iInfo['page'] : 1;
        $courses    = D('Course')->getUserCourses(C('user.id'), $page, $status);
        $this->outOInfo(array('list' => $courses));
    }

    /**
     * 用户反馈
     * @param   $feedback
     * @param   $mobile
     */
    function feedback(){
        $this->unEmptyParam(array('feedback'));
        
        $feedback   = $this->iInfo['feedback'];
        $mobile     = $this->iInfo['mobile'];

        $feedInfo   = array(
            'user_id'   => C('user.id'), 
            'mobile'    => $mobile,
            'content'   => $feedback,
            'create_time'   => time()
        );

        $result     = M('t_feedback', null)->add($feedInfo);
        if(!$result)
            $this->outOInfo($result, 0);

        $this->outOInfo('反馈成功!');
    }

    /**
     * 保存用户信息
     * @param   truename
     * @param   mobile
     */
    function changeInfo(){
        $this->unEmptyParam(['mobile', 'child_relative', 'child_sex', 'child_city', 'child_age']);

        $userInfo   = [
            'truename' => $this->request('truename'),
            'mobile'    => $this->request('mobile'),
            'child_relative'    => $this->request('child_relative'),
            'child_sex' => $this->request('child_sex'),
            'child_city'    => $this->request('child_city'),
            'child_age' => $this->request('child_age'),
            'child_school'  => $this->request('child_school'),
            'child_course_name' => $this->request('child_course_name')
        ];

        $result     = D('User')->updateUserInfo(C('user.id'), $userInfo);
        if(!$result)
            $this->response(D('User')->errMsg, 0);

        //完善信息积分获取
        $score  = 10;
        D('Score')->addScore(C('user.id'), 6, 1, $score, '完善个人信息');
        $this->response('信息更新成功!');
    }

    /**
     * 积分收支记录
     * @param   page
     */
    function scoreLog(){
        $page   = $this->iInfo['page'] ? $this->iInfo['page'] : 1;

        $dScore     = D('Score');
        $result     = $dScore->getLog(C('user.id'), $page);
        if($result === false)
            $this->outOInfo($dScore->errMsg, 0);

        $this->outOInfo(array('list' => $result));
    }

    /**
     * 获取用户二维码
     */
    function getQrCode(){
         $qrCode     = D('User')->getQRCode(C('user.id'));
         $this->outOInfo(array('info' => $qrCode));
    }

    /**
     * 获取我的二维码图片
     * @return   png图片
     */
    function getQrCodePng(){
        $Image  = new \Wechat\Util\Image();
        $Image->createQrCode(C('User'));
    }

    /**
     * 绑定用户名/手机号
     * @param   $username
     * @param   $child_level
     * @param   $verify_code
     * @return  boolean
     */
    function bindUsername(){
        $this->unEmptyParam(['username', 'child_level', 'verify_code']);

        $username   = $this->iInfo['username'];
        $childLevel     = $this->iInfo['child_level'];
        $verifyCode     = $this->iInfo['verify_code'];

        if(!is_phone($username))
            $this->outOInfo('手机号格式错误!', 0);

        //验证码检测
        $result     = SmsCode::verify($username, $verifyCode);
        if($result === false){
            $this->outOInfo('验证码错误!', 0);
        }
        
        $result     = D('User')->bindUsername(C('user.id'), $username, $childLevel);
        if($result === false){
            $this->outOInfo(D('User')->errMsg, 0);
        }

        $this->outOInfo('Oh yes!');
    }

    /**
     * 购买VIP
     * @param   $vip_type
     * @return  $payInfo or errMsg
     */
    function purchaseVip(){
        $this->unEmptyParam(['vip_id']);
        
        $vipId  = $this->request('vip_id');
        $couponCode     = $this->request('coupon_code');

        $payInfo    = D('User')->purchaseVip(C('user.id'), $vipId, $couponCode);
        if($payInfo === false){
            $this->outOInfo(D('User')->errMsg, 0);
        }

        //生成JS-SDK支付信息
        $wxPay  = new \Wechat\Util\WxPay();
        $payInfo    = $wxPay->createJsPay(
            $payInfo['id'], 
            C('GOODS_NAME.VIP'),
            $payInfo['trade_no'], 
            $payInfo['amount']*100, 
            C('user.open_id')
        );

        if(!$payInfo){
            $this->outOInfo('JS-SDK支付信息生成失败!', 0);
        }
        $this->outOInfo($payInfo);
    }

    /**
     * 获取VIP信息
     * @return  $userInfo && $vinInfo
     */
    function getVipInfo(){
        $info   = ['user_info' => C('USER'), 'vip_info' => C('VIP_TYPE')];
        $info['vip_intro']  = D('PlatInfo')->getItem('VIP_INTRODUCT');
        $this->outOInfo($info);
    }
}
